package com.guigu.dh.controller;

import com.guigu.dh.common.exception.YyghException;
import com.guigu.dh.common.result.Result;
import com.guigu.dh.common.utils.AuthContextHolder;
import com.guigu.dh.common.utils.JWTUtil;
import com.guigu.dh.pojo.user.UserInfo;
import com.guigu.dh.service.UserInfoService;
import com.guigu.dh.vo.user.LoginVO;
import com.guigu.dh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api("用户登录接口")
@RestController
@RequestMapping("/api/user/userInfo")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("登录验证接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginVO loginVO){
        // 判断是否为空
        if (loginVO == null || "".equals(loginVO.getPhone()) || "".equals(loginVO.getCode())){
            throw new YyghException("登录参数错误！",500);
        }

        // 根据手机号查询是否有账号
        UserInfo userInfo = userInfoService.findUserInfoByPhone(loginVO.getPhone());

        // 判断是否存在账号
        // 不存在则注册（添加），存在则直接验证
        if (userInfo == null) {
           // 进行添加
            UserInfo userInfo1 = new UserInfo();
            userInfo1.setPhone(loginVO.getPhone());
            userInfo1.setName(loginVO.getPhone());
            userInfo1.setStatus(1);
            userInfoService.save(userInfo1);
            userInfo = userInfo1;
        }

        // 去查询验证码是否过期
        Boolean aBoolean =  userInfoService.getPhoneInfo(userInfo.getPhone());

        // 判断是否状态为0
        if (userInfo.getStatus() == 0) {
            return Result.fail("status");
        } else if (aBoolean) {
            return Result.ok();
        }
        return Result.fail("check");
    }

    // 登录成功接口，返回token
    @PostMapping("/successLogin/{phone}")
    public Result successLogin (@PathVariable String phone) {
        // 生成token
        UserInfo userInfo = userInfoService.findUserInfoByPhone(phone);
        String token = JWTUtil.createToken(userInfo.getId(), userInfo.getPhone(),
                userInfo.getName());
        Map<String, String> map = new HashMap<>();
        map.put("token",token);
        map.put("name",userInfo.getName());
        // System.out.println(map);
        return Result.ok(map);
    }


    // 获取用户信息接口
    @GetMapping("/auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        String phone = AuthContextHolder.getPhone(request);
        UserInfo userInfo = userInfoService.findUserInfoByPhone(phone);
        return Result.ok(userInfo);
    }

    // 用户认证接口
    @PostMapping("/auth/userAuth")
    public Result userAuth (@RequestBody UserAuthVo userAuthVo,HttpServletRequest request){
        Boolean result = userInfoService.userAuth(
                AuthContextHolder.getPhone(request),
                userAuthVo);

        if (result) {
            Result.ok();
        }
        return Result.fail();
    }
}
